package com.projectxxxxxxxxxx.widget;

import java.util.TimerTask;

import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.util.Log;
import android.widget.RemoteViews;

import com.projectxxxxxxxxxx.Constants;
import com.projectxxxxxxxxxx.R;
import com.projectxxxxxxxxxx.RoboApplication;
import com.projectxxxxxxxxxx.animator.Animator;
import com.projectxxxxxxxxxx.animator.DoorAnimator;
import com.projectxxxxxxxxxx.animator.ScreenAnimator;
import com.projectxxxxxxxxxx.utils.ScheduledTask;
import com.projectxxxxxxxxxx.utils.VMRuntimeHack;

public class WidgetReceiver extends BroadcastReceiver {
	
    private final long WIDGET_UPDATE_INTERVAL = 1000;
    private ScheduledTask pingTimer;
    


    public final VMRuntimeHack RUNTIME = new VMRuntimeHack();

	@Override
	public void onReceive(Context context, Intent intent) {
        onUpdate(context, intent, loadWidgetIds(context));

        RUNTIME.trackFree(35000000);
	}

	public int[] loadWidgetIds(Context context) {
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		ComponentName componentName = new ComponentName(context, WidgetReceiver.class);
		final int[] widgetIds = appWidgetManager.getAppWidgetIds(componentName);
		return widgetIds;
	}

	public void onUpdate(final Context context, Intent intent, int[] widgetIds) {

		if (widgetIds == null || widgetIds.length == 0) {
			return;
		}

		final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        final RoboApplication application = (RoboApplication) context.getApplicationContext();
		final WidgetView widgetView = application.getWidgetView();
		final WidgetManager widgetManager = application.getWidgetManager();
		
		final int length = widgetIds.length;
		for (int i = 0; i < length; i++) {
			int widgetId = widgetIds[i];
            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);

            if (intent.getAction().equals(Constants.ACTION_UPDATE_ACTION_EYES)) {
				widgetView.updateActionEyes(views);
            
            }

            if (Animator.animatorStep == 0) {

                if (intent.getAction().equals(WidgetManager.ACTION_WIDGET_OPEN_DOOR)) {
                	if(DoorAnimator.openDoor){
                		return;
                	}
                    widgetView.renderAnimation(new DoorAnimator(context, views));
                    widgetManager.changeDoor(!DoorAnimator.openDoor);
                }else if (intent.getAction().equals(WidgetManager.ACTION_WIDGET_CLOSE_DOOR)) {
                    widgetView.renderAnimation(new DoorAnimator(context, views));
                    widgetManager.changeDoor(!DoorAnimator.openDoor);
                } else if (intent.getAction().equals(WidgetManager.ACTION_WIDGET_CHANGE_SCREEN_TO_NEXT)) {
                    widgetView.renderAnimation(new ScreenAnimator(context, views, ScreenAnimator.WIDGET_NEXT_SCREEN));
                    widgetManager.changeScreen();
                } else if (intent.getAction().equals(WidgetManager.ACTION_WIDGET_CHANGE_SCREEN_TO_PREV)) {
                    widgetView.renderAnimation(new ScreenAnimator(context, views, ScreenAnimator.WIDGET_PREV_SCREEN));
                    widgetManager.changeScreen();
                }
            }
            
            	 
            	 boolean usbCharge = false;
            	 boolean acCharge = false;
            	 
            	 if (intent.getAction().equals("android.intent.action.HEADSET_PLUG")) {
            		 if (intent.getExtras().getInt("state") == 0) { 
            			 widgetView.renderHeadsetIcon(false);
            		 } else { 
            			 widgetView.renderHeadsetIcon(true);
            		 } 
	             }
            	 
	             if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)) {
	            	 widgetView.renderSDIcon(true);
	             } else if(intent.getAction().equals(Intent.ACTION_MEDIA_UNMOUNTED)) {
	            	 widgetView.renderSDIcon(false);
	             } else if(intent.getAction().equalsIgnoreCase(Intent.ACTION_UMS_CONNECTED)){
	            	 widgetView.renderUSBIcon(true);
	             } else if(intent.getAction().equalsIgnoreCase(Intent.ACTION_UMS_DISCONNECTED)){
	            	 widgetView.renderUSBIcon(false);
	             } else if(intent.getAction().equalsIgnoreCase(Intent.ACTION_BATTERY_CHANGED)){
	            	 
	            	 if(intent.hasExtra(BatteryManager.EXTRA_PLUGGED)){
		            	 int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
		            	 usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
		            	 acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
		            	 
		            	 if(usbCharge){
		            		 widgetView.renderUSBIcon(true);
		            		 widgetView.renderPOWERIcon(true);
		            	 } else if(acCharge){
		            		 widgetView.renderUSBIcon(false);
		            		 widgetView.renderPOWERIcon(true);
		            	 } else {
		            		 widgetView.renderUSBIcon(false);
		            		 widgetView.renderPOWERIcon(false);
		            	 }
	            	 }
            }
             
	             
	        if (intent.getAction().equals(Constants.ACTION_HELP_CLOSE)){
	        	if(!WidgetView.helpOneVisible && WidgetView.helpTwoVisible){
	        		WidgetView.helpTwoVisible = false;
	        	}
	        	
	        	if(WidgetView.helpOneVisible){
	        		WidgetView.helpOneVisible = false;
	        	}
	        }
	             
            if (intent.getAction().equalsIgnoreCase(Constants.ACTION_UPDATE_ACTION_LIGHT)) {
            	views = widgetView.renderActionLight(context, views, true);
            	views = widgetView.render(context, views, widgetId);
            } else if (intent.getAction().equalsIgnoreCase(Constants.ACTION_ANIMATE)) {
                widgetView.animate(context, views);
            } else {
                views = widgetView.render(context, views, widgetId);
            }
            
            appWidgetManager.updateAppWidget(widgetId, views);
            
            if (intent.getAction().equalsIgnoreCase(Constants.ACTION_UPDATE_ACTION_LIGHT)) {
                pingTimer = new ScheduledTask.DefaultScheduledTask();
                pingTimer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        final Intent intent = new Intent(Constants.ACTION_UPDATE_ACTION_LIGHT);
                        context.sendBroadcast(intent);
                    }
                }, WIDGET_UPDATE_INTERVAL);
            }
		}
	}
}
